﻿Imports AUtility
Imports System.Data

Partial Public Class UnosRezultatP
    Inherits System.Web.UI.Page

    Dim selectStr As String
    Dim ConnectionString As String = "Data Source=217.14.208.72;Initial Catalog=mojdb;Persist Security Info=True;User ID=user;Password=moj1247"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        ConnectionString = GlobalTenisLiga.ConnectionString
        'ALog.ConnectionString = ConnectionString

        'ALog.WriteLog("TenisLiga", EventLogEntryType.Information, "ErrorFind", "_Load START", "", "", Request.FilePath)
        If (Not IsPostBack) Then
            'My.Settings.ConnectionString = "Data Source=217.14.208.72;Initial Catalog=mojdb;Persist Security Info=True;User ID=user;Password=moj1247"
            FillControls()
            'GetTable(2, 1)
        End If
        'Response.Write(GetTable(2))
    End Sub

    Private Sub FillControls()

        selectStr = String.Format("SELECT * FROM Sezona ORDER BY DatumOd DESC")
        ddlSezona.DataSource = DataAccess.GetDataTable(ConnectionString, selectStr)
        ddlSezona.DataTextField = "SezonaNaziv"
        ddlSezona.DataValueField = "SezonaId"
        ddlSezona.DataBind()

        selectStr = String.Format("SELECT * FROM Liga ORDER BY LigaNaziv")
        ddlLiga.DataSource = DataAccess.GetDataTable(GlobalTenisLiga.ConnectionString, selectStr)
        ddlLiga.DataTextField = "LigaNaziv"
        ddlLiga.DataValueField = "LigaId"
        ddlLiga.DataBind()

        FillIgraci()

        ddlDan.Text = DateTime.Now.Day.ToString()
        ddlMjesec.Text = DateTime.Now.Month.ToString()
        ddlGodina.Text = DateTime.Now.Year.ToString()

        FillLokacije()

    End Sub

    Private Sub FillIgraci()
        selectStr = String.Format("SELECT * FROM vIgrac ORDER BY ImePrezime")
        Dim pobjednikDT As DataTable = DataAccess.GetDataTable(ConnectionString, selectStr)
        Dim porazeniDT As DataTable = DataAccess.GetDataTable(ConnectionString, selectStr)

        ddlPobjednik.DataSource = pobjednikDT
        ddlPobjednik.DataTextField = "ImePrezime"
        ddlPobjednik.DataValueField = "IgracId"
        ddlPobjednik.DataBind()

        ddlPorazeni.DataSource = porazeniDT
        ddlPorazeni.DataTextField = "ImePrezime"
        ddlPorazeni.DataValueField = "IgracId"
        ddlPorazeni.DataBind()
    End Sub

    Private Sub FillLokacije()
        selectStr = String.Format("SELECT DISTINCT Lokacija FROM Susret ORDER BY Lokacija")
        Dim lokacije As DataTable = DataAccess.GetDataTable(ConnectionString, selectStr)
        Dim lokacijaRow As DataRow = lokacije.NewRow()

        lokacijaRow("Lokacija") = "Nova lokacija..."
        lokacije.Rows.Add(lokacijaRow)

        ddlLokacija.DataSource = lokacije
        ddlLokacija.DataTextField = "Lokacija"
        ddlLokacija.DataValueField = "Lokacija"
        ddlLokacija.DataBind()

    End Sub


    Protected Sub btnSpremi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSpremi.Click
        Try
            Dim valid As String = ValidateUnos()
            'If (valid = "") Then
            Spremi()
            If (Not chkStay.Checked) Then
                Response.Redirect(String.Format("ZadnjiRezultati.aspx"))
            End If
        Catch ex As Exception
            lblValidate.Visible = True
            lblValidate.Text = ex.Message
        End Try
    End Sub


    Private Function ValidateUnos() As String
        Dim errorMessage As String = ""
        Dim result As Integer

        If (ddlPobjednik.SelectedValue = ddlPorazeni.SelectedValue) Then
            errorMessage += "Ne može igrati sam sa sobom!<br/>"
            Return errorMessage
        End If

        If (Not Int32.TryParse(txtI1S1.Text, result)) Then
            errorMessage += "Krivo upisan rezultat<br/>"
        End If
        If (result > 7) Then
            errorMessage += "Krivo upisan rezultat<br/>"
        End If

        If (Not Int32.TryParse(txtI2S1.Text, result)) Then
            errorMessage += "Krivo upisan rezultat<br/>"
        End If
        If (result > 7) Then
            errorMessage += "Krivo upisan rezultat<br/>"
        End If

        If (Not Int32.TryParse(txtI1S2.Text, result)) Then
            errorMessage += "Krivo upisan rezultat<br/>"
        End If
        If (result > 7) Then
            errorMessage += "Krivo upisan rezultat<br/>"
        End If

        If (Not Int32.TryParse(txtI2S2.Text, result)) Then
            errorMessage += "Krivo upisan rezultat<br/>"
        End If
        If (result > 7) Then
            errorMessage += "Krivo upisan rezultat<br/>"
        End If

        If (txtI1S3.Text <> "" Or txtI2S3.Text <> "") Then
            If (Not Int32.TryParse(txtI1S3.Text, result)) Then
                errorMessage += "Krivo upisan rezultat<br/>"
            End If
            If (Not Int32.TryParse(txtI2S3.Text, result)) Then
                errorMessage += "Krivo upisan rezultat<br/>"
            End If
        End If

        If (errorMessage <> "") Then
            errorMessage = "Krivo upisan rezultat<br/>"
            Return errorMessage
        End If

        If (Not ValidateSet(txtI1S1.Text, txtI2S1.Text)) Then
            errorMessage = "Krivo upisan rezultat<br/>"
        End If
        If (Not ValidateSet(txtI1S2.Text, txtI2S2.Text)) Then
            errorMessage = "Krivo upisan rezultat<br/>"
        End If

        Return errorMessage
    End Function

    Private Function ValidateSet(ByVal set1 As String, ByVal set2 As String) As Boolean
        Dim set1i As Integer = Convert.ToInt16(set1)
        Dim set2i As Integer = Convert.ToInt16(set2)

        If (set1i = 7) Then
            If (set2i <> 6 And set2i <> 5) Then
                Return False
            End If
            Return True
        End If

        If (set2i = 7) Then
            If (set1i <> 6 And set1i <> 5) Then
                Return False
            End If
            Return True
        End If

        If (set1i = 6 Or set2i = 6) Then
            If (set1i + set2i < 12) Then
                Return True
            End If
            Return False
        Else
            Return False
        End If


    End Function

    Private Sub Spremi()

        Dim tl As TLDS = New TLDS

        'Susret
        Dim susretRow As TLDS.SusretRow = tl.Susret.NewSusretRow()
        'susretRow.Datum = Convert.ToDateTime(txtDatum.Text)
        susretRow.Datum = New DateTime(Convert.ToInt16(ddlGodina.SelectedItem.Text), Convert.ToInt16(ddlMjesec.SelectedItem.Text), Convert.ToInt16(ddlDan.SelectedItem.Text))
        If (txtLokacija.Visible = False) Then
            susretRow.Lokacija = ddlLokacija.Text
        Else
            susretRow.Lokacija = txtLokacija.Text
        End If
        susretRow.SezonaId = 7

        tl.Susret.AddSusretRow(susretRow)
        Dim sTA As TLDSTableAdapters.SusretTableAdapter = New TLDSTableAdapters.SusretTableAdapter
        sTA.Update(tl.Susret)

        'Igrač1Susret
        Dim susretIgracRow As TLDS.SusretIgracRow = tl.SusretIgrac.NewSusretIgracRow()
        susretIgracRow.IgracId = Convert.ToInt16(ddlPobjednik.SelectedValue)
        susretIgracRow.Set1 = Convert.ToInt16(txtI1S1.Text)
        If (txtI1S2.Text <> "") Then
            susretIgracRow.Set2 = Convert.ToInt16(txtI1S2.Text)
        End If
        If (txtI1S3.Text <> "") Then
            susretIgracRow.Set3 = Convert.ToInt16(txtI1S3.Text)
        End If
        susretIgracRow.Pobjednik = True
        susretIgracRow.SusretId = susretRow.SusretId
        tl.SusretIgrac.AddSusretIgracRow(susretIgracRow)
        Dim siTA As TLDSTableAdapters.SusretIgracTableAdapter = New TLDSTableAdapters.SusretIgracTableAdapter()
        siTA.Update(tl.SusretIgrac)


        'Igrač2Susret
        susretIgracRow = tl.SusretIgrac.NewSusretIgracRow()
        susretIgracRow.IgracId = Convert.ToInt16(ddlPorazeni.SelectedValue)
        susretIgracRow.Set1 = Convert.ToInt16(txtI2S1.Text)
        If (txtI2S2.Text <> "") Then
            susretIgracRow.Set2 = Convert.ToInt16(txtI2S2.Text)
        End If
        If (txtI2S3.Text <> "") Then
            susretIgracRow.Set3 = Convert.ToInt16(txtI2S3.Text)
        End If
        susretIgracRow.Pobjednik = False
        susretIgracRow.SusretId = susretRow.SusretId
        tl.SusretIgrac.AddSusretIgracRow(susretIgracRow)
        siTA = New TLDSTableAdapters.SusretIgracTableAdapter()
        siTA.Update(tl.SusretIgrac)

        Application("ZadnjiRezultatiP") = Nothing



    End Sub

    Private Function DobiveniSetovi(ByVal igracId As Integer, ByVal sezonaId As Integer) As Integer
        selectStr = String.Format("SELECT COUNT(*)  FROM vSusretiProtivnik WHERE(Set1 > ProtivnikSet1)AND IgracId = {0} AND SezonaId = {1}", igracId, sezonaId)
        Dim prviSetovi As Integer = DataAccess.GetInt(ConnectionString, selectStr)
        selectStr = String.Format("SELECT COUNT(*)  FROM vSusretiProtivnik WHERE(Set2 > ProtivnikSet2)AND IgracId = {0} AND SezonaId = {1}", igracId, sezonaId)
        Dim drugiSetovi As Integer = DataAccess.GetInt(ConnectionString, selectStr)
        selectStr = String.Format("SELECT COUNT(*)  FROM vSusretiProtivnik WHERE(Set3 > ProtivnikSet3)AND IgracId = {0} AND SezonaId = {1}", igracId, sezonaId)
        Dim treciSetovi As Integer = DataAccess.GetInt(ConnectionString, selectStr)

        Return prviSetovi + drugiSetovi + treciSetovi

    End Function

    Private Function IzgubljeniSetovi(ByVal igracId As Integer, ByVal sezonaId As Integer) As Integer
        selectStr = String.Format("SELECT COUNT(*)  FROM vSusretiProtivnik WHERE(Set1 < ProtivnikSet1)AND IgracId = {0} AND SezonaId = {1}", igracId, sezonaId)
        Dim prviSetovi As Integer = DataAccess.GetInt(ConnectionString, selectStr)
        selectStr = String.Format("SELECT COUNT(*)  FROM vSusretiProtivnik WHERE(Set2 < ProtivnikSet2)AND IgracId = {0} AND SezonaId = {1}", igracId, sezonaId)
        Dim drugiSetovi As Integer = DataAccess.GetInt(ConnectionString, selectStr)
        selectStr = String.Format("SELECT COUNT(*)  FROM vSusretiProtivnik WHERE(Set3 < ProtivnikSet3)AND IgracId = {0} AND SezonaId = {1}", igracId, sezonaId)
        Dim treciSetovi As Integer = DataAccess.GetInt(ConnectionString, selectStr)

        Return prviSetovi + drugiSetovi + treciSetovi

    End Function


    Private Function Odigrane(ByVal igracId As Integer, ByVal sezonaId As Integer) As Integer
        selectStr = String.Format("SELECT Count(*) AS Odigrane FROM vSusreti WHERE IgracId = {0} AND SezonaId = {1}", igracId, sezonaId)
        Return DataAccess.GetInt(ConnectionString, selectStr)
    End Function

    Private Function Pobjede(ByVal igracId As Integer, ByVal sezonaId As Integer) As Integer
        selectStr = String.Format("SELECT Count(*) AS Pobjede FROM vSusreti WHERE IgracId = {0} AND SezonaId = {1} AND Pobjednik = 1", igracId, sezonaId)
        Return DataAccess.GetInt(ConnectionString, selectStr)
    End Function

    Private Function DobiveniGemovi(ByVal igracId As Integer, ByVal sezonaId As Integer) As Integer
        selectStr = String.Format("SELECT SUM(Set1) + Sum(Set2) FROM vSusretiProtivnik WHERE IgracId = {0} AND SezonaId = {1} ", igracId, sezonaId)
        Return DataAccess.GetInt(ConnectionString, selectStr)
    End Function

    Private Function IzgubljeniGemovi(ByVal igracId As Integer, ByVal sezonaId As Integer) As Integer
        selectStr = String.Format("SELECT SUM(ProtivnikSet1) + Sum(ProtivnikSet2) FROM vSusretiProtivnik WHERE IgracId = {0} AND SezonaId = {1}", igracId, sezonaId)
        Return DataAccess.GetInt(ConnectionString, selectStr)
    End Function

    Private Function GetTable(ByVal sezonaId As Integer, ByVal ligaId As Integer) As String
        Dim outputString As String = "<B>ImePrezime Odigrane Pobjede Setovi Gemovi Bodovi</B><br/>"
        'Dim dt As DataTable = DataAccess.GetDataTable(ConnectionString, String.Format("SELECT IgracId, ImePrezime FROM vIgrac WHERE SezonaId = {0} AND LigaId = {1}", sezonaId, ligaId))
        Dim dt As DataTable = DataAccess.GetDataTable(ConnectionString, "SELECT IgracId, ImePrezime FROM vIgrac")

        Dim odigraneN As Integer
        Dim pobjedeN As Integer
        Dim gemoviD As Integer
        Dim gemoviI As Integer
        Dim setoviD As Integer
        Dim setoviI As Integer

        Dim ligaDT As DataTable = New DataTable
        ligaDT.Columns.Add("ImePrezime")
        ligaDT.Columns.Add("Odigrane")
        ligaDT.Columns.Add("Pobjede")
        ligaDT.Columns.Add("SetoviD", GetType(Integer))
        ligaDT.Columns.Add("SetoviI", GetType(Integer))
        ligaDT.Columns.Add("GemoviD", GetType(Integer))
        ligaDT.Columns.Add("GemoviI", GetType(Integer))
        ligaDT.Columns.Add("Bodovi")
        ligaDT.Columns.Add("SetoviRazlika", GetType(Integer), "SetoviD - SetoviI")
        ligaDT.Columns.Add("GemoviRazlika", GetType(Integer), "GemoviD - GemoviI")
        ligaDT.Columns.Add("SetoviDisplay", GetType(String))
        ligaDT.Columns.Add("GemoviDisplay", GetType(String))

        For Each row As DataRow In dt.Rows
            Try

                Dim igracId As Integer = Convert.ToInt32(row("IgracId"))
                odigraneN = Odigrane(igracId, sezonaId)
                pobjedeN = Pobjede(igracId, sezonaId)
                gemoviD = DobiveniGemovi(igracId, sezonaId)
                gemoviI = IzgubljeniGemovi(igracId, sezonaId)
                setoviD = DobiveniSetovi(igracId, sezonaId)
                setoviI = IzgubljeniSetovi(igracId, sezonaId)
            Catch ex As Exception

            End Try

            Dim ligaRow As DataRow
            ligaRow = ligaDT.NewRow()
            ligaRow("ImePrezime") = row("ImePrezime")
            ligaRow("Odigrane") = odigraneN
            ligaRow("Pobjede") = pobjedeN
            ligaRow("SetoviD") = setoviD
            ligaRow("SetoviI") = setoviI
            ligaRow("GemoviD") = gemoviD
            ligaRow("GemoviI") = gemoviI
            ligaRow("Bodovi") = odigraneN + 2 * pobjedeN
            ligaRow("SetoviDisplay") = ligaRow("SetoviD").ToString() + ":" + ligaRow("SetoviI").ToString()
            ligaRow("GemoviDisplay") = ligaRow("GemoviD").ToString() + ":" + ligaRow("GemoviI").ToString()


            ligaDT.Rows.Add(ligaRow)

            outputString = outputString + String.Format("{0} {1} {2} {3} {4}:{5} {6}:{7} {8}<br/>", row("ImePrezime"), odigraneN, pobjedeN, "", setoviD, setoviI, gemoviD, gemoviI, odigraneN + 2 * pobjedeN)

            odigraneN = 0
            pobjedeN = 0
            gemoviD = 0
            gemoviI = 0
            setoviD = 0
            setoviI = 0
        Next

        ligaDT.DefaultView.Sort = "Bodovi DESC, Odigrane DESC"
        gvLiga.DataSource = ligaDT
        gvLiga.DataBind()

        Return outputString
    End Function


    Protected Sub ddlLiga_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlLiga.SelectedIndexChanged
        FillIgraci()
    End Sub

    Protected Sub ddlLokacija_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlLokacija.SelectedIndexChanged
        If (ddlLokacija.SelectedItem.Text = "Nova lokacija...") Then
            txtLokacija.Visible = True
        Else
            txtLokacija.Visible = False
        End If
    End Sub

    Protected Sub ddlSezona_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlSezona.SelectedIndexChanged
        FillIgraci()
    End Sub
End Class